//
//  TF_LivingVoteView.swift
//  TalkfunSDKDemo
//
//  Created by 莫瑞权 on 2022/4/8.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit

class TF_LivingVoteView: UIView,UITableViewDelegate,UITableViewDataSource{

    @objc public weak var delegate : TF_LivingVoteViewDelegate?
    
    @objc public let scrollView = TF_LivingVoteScrollView(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
    
    let tabView = UITableView()
    let list = NSMutableArray()

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.clear
        self.addSubview(self.scrollView)
        self.scrollView.voteSuccessfulHidde.addTarget(self, action: #selector(self.voteSuccessfulHiddeClick), for: .touchUpInside)
        self.tabView.delegate = self
        self.tabView.dataSource = self
        self.scrollView.view1.addSubview(self.tabView)
        self.tabView.backgroundColor = UIColor.clear
        self.tabView.separatorStyle = .none;
        self.tabView.register(TF_LivingVoteCell.self, forCellReuseIdentifier: "TF_LivingVoteCell")
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //添加新的投票模型
    @objc func setVoteNewUI(result:TFVoteData)
    {
        self.list.insert(result, at: 0)

        let viewH:CGFloat = 433
        let viewY:CGFloat = self.frame.size.height - viewH
        if (self.scrollView.frame.origin.y != viewY ) {
            self.scrollView.frame = CGRect(x: 0, y: self.frame.size.height, width: self.frame.size.width, height: viewH)
            UIView.animate(withDuration: 1) {
                self.scrollView.frame = CGRect(x: 0, y: viewY, width: self.frame.size.width, height: viewH)
            }
        }
        self.scrollView.layer.shadowColor = UIColor(red: 0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 0.10).cgColor
      
        self.scrollView.layer.shadowOffset = CGSize(width: 0, height: -2)
        self.scrollView.layer.shadowRadius = 12
        self.scrollView.layer.shadowOpacity = 1
        self.scrollView.layer.cornerRadius = 10
        self.tabView.frame = CGRect(x: 0, y: 59, width: self.scrollView.frame.size.width, height: self.scrollView.frame.size.height - 59)
      
        self.tabView.reloadData()
    }
    
    @objc public func isShowScrollView(show:Bool)
    {
        let viewH:CGFloat = 433
        var viewY:CGFloat = 0
        if (show == true ) {
            viewY = self.frame.size.height - viewH;
        }else{
            viewY = self.frame.size.height;
        }
         
       
        if (self.scrollView.frame.origin.y != viewY ) {

            if (show) {
                self.scrollView.frame = CGRect(x: 0, y: self.frame.size.height, width: self.frame.size.width, height: viewH)
            }

            UIView.animate(withDuration: 1) {
                self.scrollView.frame = CGRect(x: 0, y: viewY, width: self.frame.size.width, height: viewH)
        
            }
            
        }

        self.scrollView.layer.shadowColor = UIColor(red: 0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 0.10).cgColor
       
        self.scrollView.layer.shadowRadius = 12
        self.scrollView.layer.shadowOpacity = 1
        self.scrollView.layer.cornerRadius = 10
    }
    
    @objc func setVoteListUI(list:NSMutableArray)

    {
        self.list.removeAllObjects()
        self.list.addObjects(from: list as! [Any])
        self.isShowScrollView(show: true)

        
        self.tabView.frame = CGRect(x: 0, y: 59, width: self.scrollView.frame.size.width, height: self.scrollView.frame.size.height - 59)
        self.tabView.reloadData()

    }
   @objc func voteSuccessfulHiddeClick()
    {
        if (self.scrollView.frame.origin.y == self.frame.size.height - self.scrollView.frame.size.height ) {
            UIView.animate(withDuration: 1) {
                self.scrollView.frame = CGRect(x: 0, y: self.frame.size.height, width: self.frame.size.width, height: self.scrollView.frame.size.height)
                self.scrollView.layer.shadowOffset = CGSize(width: 0, height: -2)
                self.scrollView.layer.shadowRadius = 12
                self.scrollView.layer.shadowOpacity = 1
                self.scrollView.layer.cornerRadius = 10
            }
        }
        
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
           return self.list.count
       }

       func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
           
           let cell = tableView.dequeueReusableCell(withIdentifier: "TF_LivingVoteCell") as! TF_LivingVoteCell
           cell.selectionStyle = .none
           
          let model = self.list[indexPath.row] as! TFVoteData
           cell.cellWidth = self.frame.size.width
           cell.model = model;
           return cell
       }
    // 设置cell高度
      func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
          return 90 + 10
      }
    
    // 选中cell后执行此方法
       func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            let model = self.list[indexPath.row] as! TFVoteData
           if self.delegate != nil && (self.delegate?.responds(to: #selector(TF_LivingVoteViewDelegate.voteDidSelectRowAtIndexPath(_:))))!{
               self.delegate?.voteDidSelectRowAtIndexPath(model)
           }
       }

    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        
        if (self.isUserInteractionEnabled == false || self.isHidden == true || self.alpha <= 0.01 ) {
            return nil
        }

        
        // 2.判断下点在不在当前控件上
        // 用pointInside方法判断点在不在控件上,point这个必须是方法调用者坐标系上的点
        if (self.point(inside: point, with: event) == false){
            return nil
        }
        // 3.从后往前遍历自己的子控件
        
        let count = self.subviews.count
        
        //倒序
        for i in (0..<count).reversed() {

        // 取出子控件
        let childV = self.subviews[i] as UIView
            
        // 把自己坐标系上点转化成子控件坐标系上点
          let  childP =  self.convert(point, to: childV)
          let fitView =  childV.hitTest(childP, with: event)
             
            // 如果找到最合适view就直接返回
            if ((fitView) != nil) {
                return fitView;
            }
            
        }
     
        if  self.scrollView.frame.origin.y == self.frame.size.height - self.scrollView.frame.size.height {
            UIView.animate(withDuration: 1) {
            self.scrollView.frame = CGRect(x: 0, y: self.frame.size.height, width: self.frame.size.width, height: self.scrollView.frame.size.height)
            self.scrollView.layer.shadowOffset = CGSize(width: 0, height: -2)
            self.scrollView.layer.shadowRadius = 12
            self.scrollView.layer.shadowOpacity = 1
            self.scrollView.layer.cornerRadius = 10
            }
        }
        return nil

    }
}
@objc protocol TF_LivingVoteViewDelegate : NSObjectProtocol {

    func voteDidSelectRowAtIndexPath(_ model : TFVoteData)

}
